package com.heima.article.listener;

import com.heima.article.service.IApArticleService;
import com.heima.common.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Optional;

import static com.heima.common.constants.message.WmNewsUpDownConstants.WM_NEWS_UP_OR_DOWN_TOPIC;

@Slf4j
@Component
public class WmNewsUpOrDownListener {

    @Autowired
    private IApArticleService apArticleService;

    @KafkaListener(topics = WM_NEWS_UP_OR_DOWN_TOPIC)
    public void receiveMsg(ConsumerRecord<?,?> record){
        //判断record是否有值
        Optional<? extends ConsumerRecord<?, ?>> optional = Optional.ofNullable(record);
        if(optional.isPresent()){
            //更新文章上下架状态
            Object value = record.value();
            log.info("上下架接收的数据为："+value);
            //解析成map数据
            Map<String, Integer> map = JsonUtils.toMap(String.valueOf(value), String.class, Integer.class);
            //更新文章上下架状态
            apArticleService.updateIsDown(map);
        }
    }
}
